/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.example.spring.entity;

import com.example.spring.utils.IdGen;
import com.example.spring.utils.UserUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.validator.constraints.Length;

import java.util.Date;

/**
 * 数据Entity类
 *
 * @version 2016-10-27
 */
public abstract class DataEntity<T> extends BaseEntity<T> {

    private static final long serialVersionUID = 1L;

    protected User creator;    // 创建者
    protected Date createTime;    // 创建日期
    protected User operator;    // 更新者
    protected Date operTime;    // 更新日期
    protected String status;    // 状态标记（1：正常；0：不正常）

    public DataEntity() {
        super();
        this.status = STATUS_NORMAL;
    }

    public DataEntity(String id) {
        super(id);
    }

    /**
     * 插入之前执行方法，需要手动重新设置 新ID
     */
    @Override
    public void preInsert() {
        if (!this.isNewRecord) {
            setId(IdGen.uuid());
        }
        // 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
        User user = UserUtils.getUser();
        this.operator = user;
        this.creator = user;
        this.operTime = new Date();
        this.createTime = this.operTime;
    }

    /**
     * 更新之前执行方法，需要手动调用
     */
    @Override
    public void preUpdate() {
        User user = UserUtils.getUser();
        this.operator = user;
        this.operTime = new Date();
    }


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getOperTime() {
        return operTime;
    }

    public void setOperTime(Date operTime) {
        this.operTime = operTime;
    }

    @Length(min = 1, max = 1)
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}
